package com.kws.merchant.service.impl;


import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.kws.common.config.constant.MinioCommonBucket;
import com.kws.common.security.auth.AuthUtil;
import com.kws.merchant.domain.OperationPolling;
import com.kws.merchant.domain.OperationWorkOrder;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kws.merchant.domain.Order;
import com.kws.merchant.domain.po.OperationWorkDisposePo;
import com.kws.merchant.domain.po.OperationWorkOrderPo;
import com.kws.merchant.mapper.OperationWorkOrderMapper;
import com.kws.merchant.service.OperationWorkOrderService;
import com.kws.merchant.util.FileUtil;
import com.kws.system.api.domain.SysUser;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import java.util.List;
import java.util.UUID;

/**
 * 运维-工单管理(OperationWorkOrder)表服务实现类
 *
 * @author yxy
 * @since 2024-10-26 12:27:15
 */
@Service("operationWorkOrderService")
public class OperationWorkOrderServiceImpl extends ServiceImpl<OperationWorkOrderMapper, OperationWorkOrder> implements OperationWorkOrderService {

    @Resource
    private OperationWorkOrderMapper operationWorkOrderMapper;
    @Value("${minio.ENDPOINT}")
    private String minioUrl;

    /**
     * 分页查询列表
     */
    @Override
    public Page<OperationWorkOrder> pageList(OperationWorkOrderPo operationWorkOrderPo) {
        Integer pageSize = operationWorkOrderPo.getPageSize();
        Integer totalPage = operationWorkOrderPo.getTotalPage();
        String workOrderNo = operationWorkOrderPo.getWorkOrderNo();//工单编号
        String serveType = operationWorkOrderPo.getServeType();//服务类型
        String priority = operationWorkOrderPo.getPriority();//优先级
        SysUser sysUser = AuthUtil.getLoginUser().getSysUser();
        String sql=" o.merchant_id="+sysUser.getCompanyId();
        if (StringUtils.isNotEmpty(workOrderNo)){
            sql+=" and o.work_order_no='"+workOrderNo+"'";
        }
        if (StringUtils.isNotEmpty(serveType)){
            sql+=" and o.serve_type="+serveType;
        }
        if (StringUtils.isNotEmpty(priority)){
            sql+=" and o.priority="+priority;
        }
        int sizeNum = (pageSize - 1) * totalPage;//计算分页
        int total = operationWorkOrderMapper.countToTal(sql);
        //查询签名数据
        List<OperationWorkOrder> records=operationWorkOrderMapper.getRecords(sql,sizeNum,totalPage);
        records.forEach(x->{
            x.setFile(FileUtil.fixFileName()+x.getFile());
        });
        Page<OperationWorkOrder> page = new Page<>();
        page.setRecords(records);
        page.setTotal(total);
        page.setCurrent(pageSize);
        page.setPages((total+totalPage-1)/totalPage);
        page.setSize(totalPage);
        return page;
    }

    /**
     * 工单处理
     * @return
     */
    @Override
    public boolean dispose(OperationWorkOrder operationWorkOrder) {
        if (operationWorkOrder.getStatus()!=1){
            throw new RuntimeException("工单不是待处理状态，无法处理！！");
        }
        operationWorkOrder.setStatus(0);
        operationWorkOrder.setFile(FileUtil.subFileName(operationWorkOrder.getFile()));
        return this.updateById(operationWorkOrder);
    }

    /**
     * 通过状态查询工单
     * @param status
     * @return
     */
    @Override
    public List<OperationWorkOrder> listByStatus(int status) {
        return this.list(Wrappers.<OperationWorkOrder>lambdaQuery().eq(OperationWorkOrder::getStatus,status));
    }

    /**
     * 新增工单
     * @param operationWorkOrder
     * @return
     */
    @Override
    public Object addWorkOrder(OperationWorkOrder operationWorkOrder) {
        Long companyId = AuthUtil.getLoginUser().getCompanyId();
        operationWorkOrder.setStatus(1);
        operationWorkOrder.setWorkOrderNo("GD"+ UUID.randomUUID());
        operationWorkOrder.setMerchantId(companyId);
        operationWorkOrder.setFile(FileUtil.subFileName(operationWorkOrder.getFile()));
        return this.save(operationWorkOrder);
    }

    /**
     * 查询商家所有工单
     * @param companyId
     * @return
     */
    @Override
    public List<OperationWorkOrder> listByMerchant(Long companyId) {
        return this.list(Wrappers.<OperationWorkOrder>lambdaQuery().eq(OperationWorkOrder::getMerchantId,companyId));
    }

    @Override
    public OperationWorkOrder detailById(Long id) {
        OperationWorkOrder workOrder = this.getById(id);
        workOrder.setFile(FileUtil.fixFileName()+workOrder.getFile());
        return workOrder;
    }

    @Override
    public Double countWorkTypeBySql(Long companyId, String sql) {
        return operationWorkOrderMapper.countWorkTypeBySql(companyId,sql);
    }


}
